<template>
  <!-- 注册页面的整体盒子 -->
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <el-form
        style="padding: 0 25px"
        :model="formdata"
        :rules="formrules"
        ref="form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="formdata.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formdata.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="formdata.repassword"
            placeholder="请确认密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="resfn()">
            注册</el-button
          >
          <el-link type="info" href="http://localhost:8080/#/">去登录</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  // name: "Reg",
  data() {
    var rePwd = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.formdata.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        // 如果验证成功，则直接调用 callback 回调函数即可。
        //跟next()相似,放行
        callback();
      }
    };
    return {
      formdata: {
        username: "",
        password: "",
        repassword: "",
      },
      formrules: {
        /* 校验规则依次校验 */
        //校验是数组,可以有多个规则
        username: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          {
            pattern: /^[a-zA-Z0-9]{1,10}$/,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern: /^\S{6,10}$/,
            message: "长度在 6到 10 个字符",
            trigger: "blur",
          },
        ],
        repassword: [
          // { required: true, message: "请确认密码", trigger: "blur" },
          {
            pattern: /^\S{6,10}$/,
            message: "长度在 6到 10 个字符",
            trigger: "blur",
          },
          //validator:调用validatePass2函数
          //调用这个的函数,与密码框中进行比较
          { validator: rePwd, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    resfn() {
      //form表单规则
      //对表单进行兜底校验
      this.$refs.form.validate(async (valid) => {
        // console.log(valid);  是一个布尔值
        //代码优化
        if (!valid) return console.log("校验失败");
        // if (valid)
        // {
        alert("提交成功");
        //成功了,就发送ajax
        //注意返回值
        const { data: res } = await this.$http.post("/api/reg", this.formdata);
        // console.log(res);
        //有了返回值要判断
        //失败提示
        if (res.code != 0) return this.$message.error(res.message);
        // {
        //成功提示
        this.$message.success(res.message);
        //成功后调转到页面,login页面
        this.$router.push("/");
        // }
        //  else {
        //   alert(res.data.message);
        // }
        // }
        //  else {
        //   console.log("错误");
        //   return false;
        // }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.reg-container {
  background: url("../../assets/images/login_bg.jpg") center;
  background-size: cover;
  height: 100%;

  .reg-box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .title-box {
      height: 60px;
      background: url("../../assets/images/login_title.png") center no-repeat;
    }
  }
}
</style>

